/**
 *
 */
package admin.mode;

import java.util.ArrayList;
import java.util.HashMap;

import base.Power;

/**
 * @author qsj
 * 
 */
public class MenuMode {
	public ArrayList<HashMap<String, String>> getTopMenuList(Power power)
			throws Exception {
		String sql = "select * from top_menu";
		return power.getListFromSql(sql);
	}

	public ArrayList<HashMap<String, String>> getLeftMenuList(Power power)
			throws Exception {
		String sql = "select * from left_menu_second";
		return power.getListFromSql(sql);
	}

	public HashMap<String, String> getLeftMenuById(Power power,
			String left_menu_id) throws Exception {
		String sql = "select * from left_menu_second where left_menu_id="
				+ left_menu_id;
		return power.getMapFromSql(sql);
	}

	public ArrayList<HashMap<String, String>> getTopMenuListByPerm(Power power,
			String roleId) throws Exception {
		String sql = "select * from `user_top_menu` t1 ,top_menu t2  where t1.users_id="
				+ roleId + " and t1.top_menu_id_users =t2.top_menu_id;";
		return power.getListFromSql(sql);
	}

	/**
	 *@author lcl
	 *@param 根据top取到左侧菜单
	 * */

	public ArrayList<HashMap<String, Object>> getLeftMenuByTopMenuId(
			Power power, String topMenuId) throws Exception {
		// 根据TOPMenu取到leftMenu的二级菜单
		String sql = "select * from `left_menu_second` t  where  t.top_menu_id="
				+ topMenuId + " order by t.left_menu_id";
		ArrayList<HashMap<String, String>> info = power.getListFromSql(sql);
		ArrayList<HashMap<String, Object>> leftMenu = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < info.size(); i++) {
			HashMap<String, String> map = info.get(i);
			HashMap<String, Object> map1 = new HashMap<String, Object>();
			map1.putAll(map);
			String menusql = "select * from left_menu_second t1,left_menu_third t2 where left_menu_id="
					+ map.get("LEFT_MENU_ID")
					+ " and t1.left_menu_id=t2.top_second_id";
			// 根据leftMenu的id取到三级菜单
			map1.put("leftThirdMenu", power.getListFromSql(menusql));
			leftMenu.add(map1);

		}
		return leftMenu;
	}

	public ArrayList<HashMap<String, String>> getLeftMenuByPermAndTopMenuId(
			Power power, String roldId, String topMenuId) throws Exception {
		String sql = "select * from leftmenu as a,role_permission as b where a.tm_id="
				+ topMenuId
				+ " and b.role_id="
				+ roldId
				+ " and substring_index(a.lm_perm,',',1)=b.uri order by lm_sort";
		return power.getListFromSql(sql);
	}

	public HashMap<String, String> getTopMenuById(Power power, String tm_id)
			throws Exception {
		String sql = "select * from topmenu where tm_id=" + tm_id;
		return power.getMapFromSql(sql);
	}

	public boolean passwordUpdate(Power power, AdminMode admin)
			throws Exception {
		String oldpassword = power.getParameter("oldpassword");
		// MD5Util cipher = new MD5Util();
		// oldpassword = cipher.generatePassword(oldpassword);
		String password = power.getParameter("password");
		// password = cipher.generatePassword(password);
		String userid = power.getParameter("userid");
		HashMap<String, String> map = power
				.getMapFromSql("select * from u_user where userid=" + userid);
		if (map.get("USERPASSWORD").equals(oldpassword)) {
			if (power.executeUpdate("update u_user set userpassword='"
					+ password + "' where userid=" + userid) > 0) {
				return true;
			}
		}
		return false;
	}

	public boolean userinfoUpdate(Power power, AdminMode admin)
			throws Exception {
		String userid = power.getParameter("userid");
		String username = power.getParameter("username");
		String email = power.getParameter("email");
		String realname = power.getParameter("realname");
		String gender = power.getParameter("gender");
		String remark = power.getParameter("remark");
		String blogurl = power.getParameter("blogurl");
		String address = power.getParameter("address");

		String sql = "update u_user set username='" + username + "',email='"
				+ email + "',remark='" + remark + "',blogurl='" + blogurl
				+ "',address='" + address + "',realname='" + realname
				+ "',sex='" + gender + "' where userid=" + userid;
		if (power.executeUpdate(sql) > -1) {
			return true;
		}
		return false;
	}

	public HashMap<String, String> getUserInfoById(Power power,
			AdminMode admin, String id) throws Exception {
		String sql = "select * from u_user where userid=" + id;
		return power.getMapFromSql(sql);
	}

}
